<template>
	<view>
		<view class="good-list-item">
			<!-- 勾选框 -->
			<radio value="r1" :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="changeRadio"/>
			<!-- 左侧图片 -->
			<view class="left-img-box">
				<image :src="goods.goods_big_logo || defaultPic" class="goods-pic"></image>
			</view>
			<!-- 右侧商品信息 -->
			<view class="right-info-box">
				<view class="good-name">
					{{goods.goods_name}}
				</view>
				<view class="good-info-box">
					<view class="good-price">
						￥{{goods.goods_price | toFixed}}
					</view>
					<!-- 商品数量 -->
					<uni-number-box :min="1" v-if="showNum" @change="changeGoodsNum" :value="goods.goods_count"></uni-number-box>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"cus-good",
		props: {
			goods: {
				type: Object,
				default: {}
			},
			// 是否展示勾选框
			showRadio: {
				type: Boolean,
				default: false
			},
			// 是否展示商品数量
			showNum: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				// 默认图片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
		
		methods: {
			// 修改勾选框状态
			changeRadio() {
				this.$emit('changeRadio', {
					goods_id: this.goods.goods_id,
					goods_state: !this.goods.goods_state
				})
			},
			
			// 修改商品数量
			// uni-number-box组件 ，change函数的参数为当前输入框的值
			changeGoodsNum(value) {
				this.$emit('changeNum', {
					goods_id: this.goods.goods_id,
					goods_count: value - 0  // 保证是数字
				})
				// console.log(val);
			}
		},
		
		filters: {
			toFixed(num) {
				return num.toFixed(2)
			}
		}
	}
</script>

<style lang="scss">
.good-list-item {
		display: flex;
		align-items: center;
		padding: 10px 5px;
		border-bottom: 1px solid #f0f0f0;

		.left-img-box {
			margin-right: 5px;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.right-info-box {
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: space-between;
			.good-info-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.good-name {
					font-size: 14px;
				}
				
				.good-price {
					font-size: 16px;
					color: #c00000;
				}
			}
			
		}
	}
</style>